package statement;

import java.util.Scanner;

public class ControlFlowExercise {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 菜单驱动程序
        while (true) {
            System.out.println("\n===== 数字处理程序 =====");
            System.out.println("1. 判断奇偶数");
            System.out.println("2. 计算阶乘");
            System.out.println("3. 打印乘法表");
            System.out.println("4. 查找素数");
            System.out.println("5. 退出");
            System.out.print("请选择功能(1-5): ");

            int choice = scanner.nextInt();

            // 使用switch处理菜单选择
            switch (choice) {
                case 1:
                    checkEvenOdd(scanner);
                    break;
                case 2:
                    calculateFactorial(scanner);
                    break;
                case 3:
                    printMultiplicationTable(scanner);
                    break;
                case 4:
                    findPrimeNumbers(scanner);
                    break;
                case 5:
                    System.out.println("程序退出，再见！");
                    scanner.close();
                    return; // 结束程序
                default:
                    System.out.println("无效的选择，请重试");
            }
        }
    }

    // 判断奇偶数
    private static void checkEvenOdd(Scanner scanner) {
        System.out.print("请输入一个整数: ");
        int num = scanner.nextInt();

        if (num % 2 == 0) {
            System.out.println(num + " 是偶数");
        } else {
            System.out.println(num + " 是奇数");
        }
    }

    // 计算阶乘
    private static void calculateFactorial(Scanner scanner) {
        System.out.print("请输入一个正整数: ");
        int n = scanner.nextInt();

        if (n < 0) {
            System.out.println("请输入非负整数");
            return;
        }

        long factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        System.out.println(n + " 的阶乘是: " + factorial);
    }

    // 打印乘法表
    private static void printMultiplicationTable(Scanner scanner) {
        System.out.print("请输入乘法表的大小(1-10): ");
        int size = scanner.nextInt();

        if (size < 1 || size > 10) {
            System.out.println("请输入1-10之间的数字");
            return;
        }

        System.out.println("===== " + size + "x" + size + " 乘法表 =====");
        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size; j++) {
                System.out.printf("%4d", i * j);
            }
            System.out.println();
        }
    }

    // 查找素数
    private static void findPrimeNumbers(Scanner scanner) {
        System.out.print("请输入一个上限值: ");
        int limit = scanner.nextInt();

        if (limit < 2) {
            System.out.println("请输入大于1的整数");
            return;
        }

        System.out.println("2到" + limit + "之间的素数有:");
        int count = 0;

        // 外层循环遍历每个数字
        outer:
        for (int num = 2; num <= limit; num++) {
            // 内层循环判断是否为素数
            for (int i = 2; i <= Math.sqrt(num); i++) {
                if (num % i == 0) {
                    continue outer; // 不是素数，继续外层循环的下一个数
                }
            }

            // 如果是素数，打印出来
            System.out.print(num + " ");
            count++;
            // 每5个素数换一行
            if (count % 5 == 0) {
                System.out.println();
            }
        }
        System.out.println("\n共找到 " + count + " 个素数");
    }
}
